package com.mtsmda.webServices.rpcStyle.attach;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.jws.WebService;
import javax.xml.ws.soap.MTOM;

@MTOM
@WebService(endpointInterface = "com.mtsmda.webServices.rpcStyle.attach.WServiceRPCAttachI")
public class WServiceRPCAttachImpl implements WServiceRPCAttachI {

	public Image downloadImage(String name) {
		try {
			File file = new File("koshak.jpg");

			Image im = ImageIO.read(file);
			return im;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String uploadImage(Image imageUpload) {
		try {
			if (imageUpload != null) {
				File file = new File("koshak.jpg");
				ImageIO.write(toBufferedImage(imageUpload), "jpg", file);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static BufferedImage toBufferedImage(Image image) {
		if (image instanceof BufferedImage) {
			return (BufferedImage) image;
		}

		BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),
				image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
		Graphics2D createGraphics = bufferedImage.createGraphics();
		createGraphics.drawImage(image, 0, 0, null);
		createGraphics.dispose();
		return bufferedImage;
	}
}